const app = getApp();

// pages/user/manager/category/category.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    categoryName: '',
    categorySort: null,
    status: null,
    categoryId: null,
    selectIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },

  init(){
    var _this = this
    app.request('/category/all', 'GET', {})
      .then(data => {
        if(data.code === 0){ 
          if(data.allCategory.length === 0){
            data.allCategory.push({categoryName: "", categorySort: 1})
          }
          _this.setData({
            categoryList: data.allCategory, 
          })
          var activeKey = _this.data.activeKey;
          if(activeKey != null){
            _this.switchSidebar(activeKey);
          }else{
            _this.switchSidebar(0);
          }
        }else{
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
    }).catch(err => {
      wx.showToast({
        title: '服务繁忙',
        icon: 'none'
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  addCategory(){
    var _this = this
    var categoryList = _this.data.categoryList;
    categoryList.push({categoryName: ""})
    _this.setData({
      categoryList: categoryList
    })
    _this.switchSidebar(categoryList.length - 1);
  },

  switchSidebar(index){
    var _this = this
    var categoryList = _this.data.categoryList;
    var categoryName = categoryList[index].categoryName;
    var categoryId = categoryList[index].categoryId;
    var status = categoryList[index].status;
    _this.setData({ 
      activeKey: index, 
      categoryName: categoryName, 
      categoryId: categoryId , 
      status: status 
    });
  },

  updateCategory(){
    var _this = this
    var categoryName = _this.data.categoryName;
    var categoryId = _this.data.categoryId;
    var categorySort = _this.data.categorySort;
    if(app.isEmptyString(categoryName)){
      wx.showToast({
        title: '请输入类别名称',  
        icon: 'none'
      })
      return;
    }
    _this.update( {
      categoryName: categoryName,
      categoryId: categoryId,
      categorySort: categorySort,
    });
  },

  update(data){
    var _this = this
    app.request('/category/update', 'POST', data)
      .then(data => {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        _this.init();
    }).catch(err => {
      wx.showToast({
        title: '服务繁忙',
        icon: 'none'
      })
    });
  },

  updateStatus(){ 
    var _this = this
    var categoryId = _this.data.categoryId; 
    var status = _this.data.status; 
    status = status == 0 ? 1 : 0;
    if(categoryId == null || categoryId == undefined){
      wx.showToast({
        title: '请选择类别',  
        icon: 'none'
      })
      return;
    }
    _this.update( {
      categoryId: categoryId,
      status: status,
    });
  },

  // 输入框输入事件
  onCategoryNameInput(e) {
    this.setData({
      categoryName: e.detail
    });
  },

  onSidebarChange(event) {
    var _this = this
    var index = event.detail; // 当前选中的索引
    _this.switchSidebar(index);
  },

  moveUp(){
    var _this = this
    var activeKey = _this.data.activeKey;
    var categoryList = _this.data.categoryList;
    if(activeKey == 0){ 
      return;
    }
    var upId = categoryList[activeKey].categoryId
    var downId = categoryList[activeKey - 1].categoryId
    app.request('/category/switchSort', 'POST', {upId: upId, downId: downId})
      .then(data => {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        _this.init();
        _this.switchSidebar(activeKey - 1); 
    }).catch(err => {
      wx.showToast({
        title: '服务繁忙',
        icon: 'none'
      })
    });
  },

  moveDown(){
    var _this = this
    var activeKey = _this.data.activeKey;
    var categoryList = _this.data.categoryList;
    if(activeKey == categoryList.length){ 
      return;
    }
    var upId = categoryList[activeKey].categoryId
    var downId = categoryList[activeKey + 1].categoryId
    app.request('/category/switchSort', 'POST', {upId: upId, downId: downId})
      .then(data => {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        _this.init();
        _this.switchSidebar(activeKey + 1);
    }).catch(err => {
      wx.showToast({
        title: '服务繁忙',
        icon: 'none'
      })
    });
  },

  deleteCategory(){
    var _this = this
    var categoryId = _this.data.categoryId; 
    _this.update( {categoryId: categoryId, isDeleted: 1});
  }
})